package pkg

import (
	log2 "chess/log"
	"context"
	"errors"
	"fmt"
	"strconv"
)

const SIZE = 10
const WinCount = 5
const WinCountWarring = 3

var BoardMap = make(map[int][SIZE][SIZE]string)

//var chessBoard [SIZE][SIZE]string

// CreateBoard 创建棋盘
func CreateBoard(roomId int) (err error) {
	_, ok := BoardMap[roomId]
	if ok {

		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "CreateBoard", strconv.Itoa(roomId), "Debug", "The board has been created")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return errors.New("the board has been created")
	}
	var chessBoard = [SIZE][SIZE]string{}
	for i := 0; i < SIZE; i++ {
		for j := 0; j < SIZE; j++ {
			chessBoard[i][j] = "+"
		}
	}
	BoardMap[roomId] = chessBoard
	return err
}

// PrintBoard 打印棋盘
func PrintBoard(roomId int) ([SIZE][SIZE]string, error) {
	value, ok := BoardMap[roomId]
	if !ok {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PrintBoard", strconv.Itoa(roomId), "Debug", "Checkerboard does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return value, errors.New("棋盘不存在")
	}
	for i := 0; i < SIZE; i++ {
		fmt.Printf("\t%d", i)
	}
	fmt.Println()
	for i := 0; i < SIZE; i++ {
		fmt.Printf("%d\t", i)
		for j := 0; j < SIZE; j++ {
			fmt.Printf("%s\t", value[i][j])
		}
		fmt.Println()
	}
	var err error
	return value, err
}

// PlayChess 玩游戏
func PlayChess(roomId int, x int, y int, isBlack bool) ([SIZE][SIZE]string, error) {
	value, ok := BoardMap[roomId]
	if !ok {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChess", "roomid:"+strconv.Itoa(roomId), "Debug", "Checkerboard does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return value, errors.New("checkerboard does not exist")
	}

	if value[x][y] != "+" {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChess", "roomid:"+strconv.Itoa(roomId), "Debug", "Please play the chess piece again,"+" Coordinate:("+strconv.Itoa(x)+","+strconv.Itoa(y)+")")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return value, errors.New("please play the chess piece again")
	}
	if x >= SIZE || y >= SIZE {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "PlayChess", "roomid:"+strconv.Itoa(roomId), "Debug", "It's out of bounds. Please try again!"+" Coordinate:("+strconv.Itoa(x)+","+strconv.Itoa(y)+")")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return value, errors.New("it's out of bounds. Please try again")
	}
	if value[x][y] == "+" && x < SIZE && y < SIZE {
		if isBlack {
			value[x][y] = "●"
		} else {
			value[x][y] = "○"
		}
	}
	BoardMap[roomId] = value
	var err error
	return value, err
}

// WinCheck 判断胜负
func WinCheck(roomId int, x int, y int) (isWin bool, err error) {

	//	存储当前有多少个棋子连线
	count := 1

	//	获取对应的棋盘
	value, ok := BoardMap[roomId]
	if !ok {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "WinCheck", "roomid:"+strconv.Itoa(roomId), "Debug", "Checkerboard does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return false, errors.New("checkerboard does not exist")
	}
	//	存储当前是什么棋子
	currentCheck := value[x][y]
	//遍历上方向
	for i := x - 1; i >= 0; i-- {
		if value[i][y] == currentCheck {
			count++
		} else {

			break
		}
	}

	//	向下遍历
	for i := x + 1; i < SIZE; i++ {
		if value[i][y] == currentCheck {
			count++
		} else {

			break
		}
	}
	if count >= WinCount {
		isWin = true
		return isWin, err
	} else {

		count = 1
	}
	//	向 左 遍历
	for j := y - 1; j >= 0; j-- {
		if value[x][j] == currentCheck {
			count++
		} else {
			break
		}
	}

	//	向右遍历
	for j := y + 1; j <= 9; j++ {
		if value[x][j] == currentCheck {
			count++
		} else {
			//count = 1
			break
		}
	}
	if count >= WinCount {
		isWin = true
		return isWin, err
	} else {

		count = 1
	}

	//	向\下遍历

	tempY := y
	for i := x + 1; i < SIZE; i++ {
		tempY++
		if tempY >= SIZE {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}

	//	向\上遍历
	tempY = y
	for i := x - 1; i >= 0; i-- {
		tempY--
		if tempY < 0 {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	if count >= WinCount {
		isWin = true
		return isWin, err
	} else {
		count = 1
	}
	//	向/上遍历
	tempY = y
	for i := x - 1; i >= 0; i-- {
		tempY++
		if tempY >= SIZE {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	//向/下遍历
	tempY = y
	for i := x; i < SIZE; i++ {
		tempY--
		if tempY < 0 {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	if count >= WinCount {
		isWin = true
		return isWin, err
	} else {
		count = 1
	}
	return false, err
}

// WinCheckWarring 判断胜负
func WinCheckWarring(roomId int, x int, y int) (Warring int, err error, EvenSubtype string) {

	//	存储当前有多少个棋子连线
	count := 1

	//	获取对应的棋盘
	value, ok := BoardMap[roomId]
	if !ok {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "WinCheck", "roomid:"+strconv.Itoa(roomId), "Debug", "Checkerboard does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, errors.New("checkerboard does not exist"), ""
	}
	//	存储当前是什么棋子
	currentCheck := value[x][y]
	//遍历上方向
	for i := x - 1; i >= 0; i-- {
		if value[i][y] == currentCheck {
			count++
		} else {

			break
		}
	}

	//	向下遍历
	for i := x + 1; i < SIZE; i++ {
		if value[i][y] == currentCheck {
			count++
		} else {

			break
		}
	}
	if count >= WinCountWarring {

		return count, err, "straight"
	} else {

		count = 1
	}
	//	向 左 遍历
	for j := y - 1; j >= 0; j-- {
		if value[x][j] == currentCheck {
			count++
		} else {
			break
		}
	}

	//	向右遍历
	for j := y + 1; j <= 9; j++ {
		if value[x][j] == currentCheck {
			count++
		} else {
			//count = 1
			break
		}
	}
	if count >= WinCountWarring {

		return count, err, "horizontal"
	} else {

		count = 1
	}

	//	向\下遍历

	tempY := y
	for i := x + 1; i < SIZE; i++ {
		tempY++
		if tempY >= SIZE {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}

	//	向\上遍历
	tempY = y
	for i := x - 1; i >= 0; i-- {
		tempY--
		if tempY < 0 {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	if count >= WinCountWarring {

		return count, err, "positiveSlope"
	} else {
		count = 1
	}
	//	向/上遍历
	tempY = y
	for i := x - 1; i >= 0; i-- {
		tempY++
		if tempY >= SIZE {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	//向/下遍历
	tempY = y
	for i := x; i < SIZE; i++ {
		tempY--
		if tempY < 0 {
			break
		}
		if value[i][tempY] == currentCheck {
			count++
		} else {
			break
		}
	}
	if count >= WinCountWarring {

		return count, err, "anticlinal"
	} else {
		count = 1
	}
	return 1, err, "nil"
}
